package edu.guigu.networkdemomvvm.single;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import edu.guigu.networkdemomvvm.network.GithubClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserRequestViewModel extends ViewModel {

    // TODO: 请求成功的LiveData数据
    public MutableLiveData<SingleUser> responeLiveData = new MutableLiveData<>();

    // TODO: 请求失败的LiveData数据
    public MutableLiveData<String> failuerLiveData = new MutableLiveData<>();

    public void remoteGetUser(int id){
        Call<SingleUser> call = GithubClient.getInstance().getUser(id);
        call.enqueue(new Callback<SingleUser>() {
            @Override
            public void onResponse(Call<SingleUser> call, Response<SingleUser> response) {
                SingleUser user = response.body();
                if (user == null){
                    failuerLiveData.postValue("解析失败");
                    return;
                }
                responeLiveData.postValue(response.body());
            }

            @Override
            public void onFailure(Call<SingleUser> call, Throwable t) {
                failuerLiveData.postValue(t.getMessage());
            }
        });
    }

}
